package lancooUpLoad;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Handler;

import com.example.lancooznbk.presenter.presenter_interface.IBaseReviewPresenter;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import testPaperCTCS.GuessTestingPaperActivity;
import testPaperWrong.WrongQuesPaperActivity;

import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;

/**
 * Created by Administrator on 2017/9/5/005.
 */

public class SubmitWrongPresenter implements IBaseReviewPresenter {

    private ILoadPaperView view;
    private int PaperFrom;
    private boolean isFromExit = false;
    private Dialog mDialog;

    public SubmitWrongPresenter(ILoadPaperView view, int paperFrom) {
        this.view = (ILoadPaperView) view;
        this.PaperFrom = paperFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<SubmitAnswerEntityResult> httpOnNextListener = new HttpOnNextListener<SubmitAnswerEntityResult>() {

            @Override
            public void onNext(SubmitAnswerEntityResult submitAnswerEntityResult) {


                if(submitAnswerEntityResult.getValue().equals("1")){



                    if (isFromExit){

                        view.getLoadingDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                InforWrongQues.get(0).setState(2);
                                view.openExamReportDialog();
                            }
                        });
                       /* new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                InforWrongQues.get(0).setState(2);
                                view.openExamReportDialog();
                            }
                        },600);*/

                    }

                    view.updatingResult(true);
                }else{
                    view.updatingResult(false);
                    ToastUtils.showShort("数据保存失败! 错误码"+submitAnswerEntityResult.getValue());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.updatingResult(false);
            }
        };


        SubmitWrongParam  api = new SubmitWrongParam(httpOnNextListener, (WrongQuesPaperActivity) view);

        api.setListSubmitDataWrongJson(PaperInfor.ListSubmitDataWrongJson);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit){
        this.isFromExit = isFromExit;
        getData(isShowing);
    }

}